<?php
namespace app\admin\controller;
use app\admin\model\Basic as BasicModel;
use app\admin\model\Ad as AdModel;
use think\Validate;
use think\Cache;
use think\Db;

/**
 * 系统管理模块
 * @package app\admin\controller
 */
class Basic extends Base
{
    /**
     * 基本设置
     * @author 陈一中
     * @return mixed
     */
    public $config;
    public function index()
    {
        // 获取基本设置信息
        $data = Db::name('config')->select();
        foreach ($data as $k => $v) {
            $info[$v['name']] = $v;
            //$info[$v['type']][$v['name']] = $v;
        }
        session('config',$info);
        $this->assign('info',$info);
        return  $this->fetch();
    }

    /**
     * 更新基本设置
     * @author 陈一中 <[email address]>
     * @return [type] [description]
     */
    public function update_basic()
    {

        $bool =false;
        if (request()->isPost()) {
            $info = Request::instance()->param();
            var_dump($info);die;

            $file = request()->file('thumb');
            if (!empty($file)) {
                $upload_path = Config::get('upload_path');
                $file_info = $file->move('.' . $upload_path['index']);
                if ($file_info) {
                    $info['logo'] = $upload_path['index'] . $file_info->getSaveName();
                }
            }
            foreach ($info as $k => $v) {
                $data['value'] = $v;
                $data['up_time'] = time();
                $bool = Db::name('config')->where('name',$k)->update($data);
            }
        }
        if ($bool) {

            return $this->success('更新成功', '/basic/index.html');
            die;
        }
        else{
            return $this->error('更新失败');
            die;
        }       
    }

    /**
     * 广告列表
     * @author 陈一中 <[email address]>
     * @return [type] [description]
     */
    public function ad()
    {
        $info = $this->request->post();
        $id = isset($info['id']) ? $info['id'] : 0;
        if ($id > 0) {
            $where['position_id'] = $id;
        }else{
            $where = '';
        }
        $data = Db::name('ad')->where($where)->paginate(10,false,['query'=>$info]);
        $page = $data->render();

        $position = Db::name('position')->where('type',1)->select();
        $this->assign('position',$position);
        $info['id'] = $id; 
        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('info',$info);
        $this->assign('meta_title', '广告管理');
        return $this->fetch();
    }

    /**
     * 广告添加
     * @author 陈一中 <[email address]>
     */
    public function add_ad()
    {
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            // $result = $this->validate($data, 'Article');
            // 验证失败 输出错误信息
            // if(true !== $result) return $this->error($result);
            // 添加数据
            if ($article = AdModel::create($data)) {
                // 记录行为
                action_log('ad_update', 'ad', $article['id'], UID);
                return $this->success('新增成功', cookie('__forward__'));
            } else {
                return $this->error('新增失败');
            }
        }
        $position = Db::name('position')->where('status',1)->select();
        $this->assign('position',$position);
        $this->assign('meta_title', '广告管理');
        return  $this->fetch();
    }
    /**
     * 广告添加
     * @author 陈一中 <[email address]>
     */
    public function ad_edit($id=0)
    {
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            // $result = $this->validate($data, 'Article');
            // 验证失败 输出错误信息
            // if(true !== $result) return $this->error($result);
            // 添加数据
            if ($article = AdModel::update($data)) {
                // 记录行为
                action_log('ad_update', 'ad', $article['id'], UID);
                return $this->success('新增成功', cookie('__forward__'));
            } else {
                return $this->error('新增失败');
            }
        }
        $position = Db::name('position')->where('status',1)->select();
        $info = AdModel::get($id);
        $this->assign('info',$info);
        $this->assign('position',$position);
        $this->assign('meta_title', '广告管理');
        return  $this->fetch();
    }
    /**
     * 短信设置
     * @author 陈一中 <[email address]>
     * @return [type] [description]
     */
    public function sms()
    {
        $info = session('config');
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 支付设置
     * @author 陈一中 <[email address]>
     * @return [type] [description]
     */
    public function pay()
    {
        $info = session('config');
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 其他设置
     * @author 陈一中 <[email address]>
     * @return [type] [description]
     */
    public function other()
    {
        $info = session('config');
        $this->assign('info',$info);
        return $this->fetch();
    }

}
